package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.EmployeeService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @PostMapping("/add")
    public Result<Employee> add(@Validated @RequestBody Employee data) {
        log.debug("收到add请求，请求参数：{}", data);
        return Result.ok(employeeService.add(data));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable int id) {
        log.debug("收到delete请求，请求参数：{}", id);
        employeeService.delete(id);
        return Result.ok(null);
    }

    @PatchMapping("/update")
    public Result<?> update(@Validated @RequestBody Employee data) {
        log.debug("收到update请求，请求参数：{}", data);
        employeeService.update(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(employeeService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<Employee>> getList(@RequestBody EmployeeDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<Employee> res = employeeService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @PostMapping("/login")
    public Result<Login> login(@RequestBody Employee data) {
        return Result.ok(employeeService.login(data));
    }

    @PostMapping("/isAdmin")
    public Result<?> isAdmin(@RequestBody String num){
        employeeService.isAdmin(num);
        return Result.ok(null);
    }

    @GetMapping("/getPower")
    public Result<List<EmployeePowerVO>> getUserPowerList(){
        List<EmployeePowerVO> list = employeeService.getPowerListByEmployeeId();
        return Result.ok(list);
    }
    //getCurrentUser
    @GetMapping("/getCurrentUser")
    public Result<Employee> getCurrentUser() {
        log.debug("收到select请求！");
        return Result.ok(employeeService.getCurrentUser());

    }

    @PatchMapping("/updatePwd")
    public Result<?> updatePwd(@RequestBody Employee data) {
        log.debug("收到update请求，请求参数：{}", data);
        employeeService.updatePwd(data);
        return Result.ok(null);
    }
}
